/**
 * MeetSourceAction.java
 */
package com.prs.crm.action.meeting;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.District;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.meeting.MeetSource;
import com.prs.crm.service.meeting.MeetSourceService;

/**
 * @author ZhuDongMei
 * @E-mail:1223984339@qq.com
 * @qq:1223984339
 * @version 创建时间：2011-3-16 上午11:12:28 简单说明
 */

public class MeetSourceAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer[] meetSourceIds;
	private MeetSource meetSource;
	private PaginationSupport pagination;
	private MeetSourceService meetSourceService;
	private Collection<TreeNode> nodes;
	private TreeNode root;

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getMeetSourceIds() {
		return meetSourceIds;
	}

	public void setMeetSourceIds(Integer[] meetSourceIds) {
		this.meetSourceIds = meetSourceIds;
	}

	public MeetSource getMeetSource() {
		return meetSource;
	}

	public void setMeetSource(MeetSource meetSource) {
		this.meetSource = meetSource;
	}

	public MeetSourceService getMeetSourceService() {
		return meetSourceService;
	}
	public void setMeetSourceService(MeetSourceService meetSourceService) {
		this.meetSourceService = meetSourceService;
	}
	@Action(value = "loadSourceTree", results = { @Result(name = "success", type = "json", params = {
			"root", "root" }) })
	public String loadSourceTree() {
		root = new TreeNode();
		root.setId("-1");

		TreeNode firstNode = new TreeNode();
		firstNode.setId("0");
		firstNode.setText("资源选择");

		Collection<MeetSource> meetSources = this.getMeetSourceService().getRootMeetSources();

		for (MeetSource meetSource : meetSources) {
			TreeNode node = new TreeNode();
			node.setText(meetSource.getName());
			node.setId("source-" + meetSource.getId().toString());
			firstNode.addChildNode(node);
			addDepartNode(node, meetSource);
			
		}

		root.addChildNode(firstNode);
		return SUCCESS;
	}
	private void addDepartNode(TreeNode parent, MeetSource meetSource) {
		for (MeetSource source : meetSource.getChilds()) {
			TreeNode node = new TreeNode();
			node.setText(source.getName());
			node.setId("meetSource-" + source.getId().toString());
			parent.addChildNode(node);
			addDepartNode(node, source);
			
		}
	}
	@Action("listMeetSource")
	public String listMeetSource() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetSource.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetSourceService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editMeetSource")
	public String editMeetSource() {
		return SUCCESS;
	}
	@Action(value = "saveMeetSource", results = {
			@Result(name = "success", type = "redirect", location = "listMeetSource"),
			@Result(name = "error", type = "chain", location = "editMeetSource") })
	public String saveMeetSource() {
		if (meetSource.getName() == null || "".equals(meetSource.getName().trim())) {
			this.addActionError("资源名称不能为空！");
			return ERROR;
		}
		this.getMeetSourceService().save(meetSource);
		return SUCCESS;
	}

	@Action(value = "removeMeetSource", results = {
			@Result(name = "success", type = "redirect", location = "listMeetSource"),
			@Result(name = "error", type = "chain", location = "listMeetSource") })
	public String removeMeetSource() {
		if (meetSourceIds == null) {
			this.addActionError("请选择要删除的资源名称！");
			return ERROR;
		}
		this.getMeetSourceService().batchRemove(this.getMeetSourceIds());
		return SUCCESS;
	}
}
